# Contributor: A. Wilcox <awilfox@adelielinux.org>
# Maintainer: Ariadne Conill <ariadne@dereferenced.org>

# TODO: support the GUI installer, wizard, etc.  For now, it's just the core
# image mastering tools.  Upstream work will be needed to adapt Horizon for
# installing Alpine.  (Also, we need to teach it about apkovls still.)

pkgname=horizon
pkgver=0.9.6
pkgrel=9
pkgdesc="installation environment and image mastering tools for APK-based distributions"
url="https://horizon.adelielinux.org/"
arch="all !s390x"
options="!check"  # Unpackaged dependency ruby-aruba.
license="AGPL-3.0-or-later"
checkdepends="ruby-aruba ruby-rspec valgrind"
makedepends="bcnm-dev boost-dev cmake curl-dev eudev-dev libarchive-dev
	linux-headers parted-dev skalibs-dev util-linux-dev libxkbfile-dev
	samurai"
subpackages="$pkgname-image $pkgname-dbg $pkgname-dev $pkgname-doc
	$pkgname-tools"
source="https://git.adelielinux.org/adelie/horizon/-/archive/$pkgver/horizon-$pkgver.tar.gz
	json-cast-character-to-unsigned-for-comparrison.patch
	riscv.patch
	strerror.patch
	werror.patch
	"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DBUILD_SHARED_LIBS=True \
		-DBUILD_TOOLS=ON \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS -Wno-format-truncation" \
		-DINSTALL=ON \
		$CMAKE_CROSSOPTS \
		-Bbuild
	cmake --build build
}

check() {
	CTEST_OUTPUT_ON_FAILURE=TRUE ninja -C build test
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# horizon is not yet supported for building ISOs, so delete it for now
	rm -rf "$pkgdir"/usr/share/horizon/iso
}

image() {
	pkgdesc="Tools for generating images using HorizonScript"
	depends="mtools"
	mkdir -p "$subpkgdir"/usr/bin
	mkdir -p "$subpkgdir"/usr/lib
	mkdir -p "$subpkgdir"/usr/share/horizon

	mv "$pkgdir"/usr/bin/hscript-image "$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/lib/libhi-backends.so "$subpkgdir"/usr/lib/
#	mv "$pkgdir"/usr/share/horizon/iso "$subpkgdir"/usr/share/horizon/
}

qt5() {
	pkgdesc="Qt 5 installation wizard for Adélie Linux"
	depends="horizon"
	mkdir -p "$subpkgdir"/etc/horizon
	mkdir -p "$subpkgdir"/usr/bin
	mkdir -p "$subpkgdir"/var/log/horizon

	for _bin in horizon-ppc64-detect horizon-qt5 horizon-run-qt5; do
		mv "$pkgdir"/usr/bin/$_bin "$subpkgdir"/usr/bin/
	done
}

tools() {
	pkgdesc="Tools for authoring HorizonScript files"
	mkdir -p "$subpkgdir"/usr/bin

	for _bin in hscript-fromjson hscript-simulate hscript-validate; do
		mv "$pkgdir"/usr/bin/$_bin "$subpkgdir"/usr/bin/
	done
}

wizard() {
	pkgdesc="Wizard to generate a HorizonScript for another computer"

	make DESTDIR="$subpkgdir" -C "$builddir"/build-wizard install
	rm -r "$subpkgdir"/usr/include
	rm -r "$subpkgdir"/usr/lib
	rm -r "$subpkgdir"/usr/share
}

boot() {
	pkgdesc="Boot to Horizon"
	depends="horizon-qt5 horizon kwin qt5ct sddm
	netsurf partitionmanager xterm"

	mkdir -p "$subpkgdir"/etc/profile.d
	mkdir -p "$subpkgdir"/etc/sddm.conf.d
	mkdir -p "$subpkgdir"/etc/xdg/qt5ct
	mkdir -p "$subpkgdir"/usr/bin
	mkdir -p "$subpkgdir"/usr/share/qt5ct/qss
	mkdir -p "$subpkgdir"/usr/share/xsessions

	cat >"$subpkgdir"/etc/profile.d/horizon-qt5.sh <<PROFILE
#!/bin/sh
export QT_QUICK_BACKEND=software
export QT_QPA_PLATFORMTHEME=qt5ct
PROFILE
	chmod 755 "$subpkgdir"/etc/profile.d/horizon-qt5.sh

	install -D -m644 "$builddir"/boot/horizon.sddm.conf \
		"$subpkgdir"/etc/sddm.conf.d/horizon.conf

	install -D -m755 "$builddir"/boot/start-horizon-qt5 \
		"$subpkgdir"/usr/bin/start-horizon-qt5

	install -D -m644 "$builddir"/boot/qt5ct.conf \
		"$subpkgdir"/etc/xdg/qt5ct/qt5ct.conf
	install -D -m644 "$builddir"/boot/horizon.qss \
		"$subpkgdir"/usr/share/qt5ct/qss/horizon.qss

	install -D -m644 "$builddir"/boot/horizon-session.desktop \
		"$subpkgdir"/usr/share/xsessions/horizon.desktop
}

sha512sums="
3ff46980387454f85a326ac3ea123b55cf16fc339faec48fdc77f10334e0bd93e8cf51e6adeed516d2e4309cf4426b9d1ac6760cdd479b196a5f4aa33c1f3a28  horizon-0.9.6.tar.gz
5ab7a35d239210e031739c4149fd8e68570ea0efd278a2eac9330d6e30c601d1310b9c17523563a41424c2a8aa2452e5a9bcbce5d8087330d2697cd1b77b312f  json-cast-character-to-unsigned-for-comparrison.patch
2748d443bdec9bfcab3a50d6efd61e3febb195d25581b25a6cbf1a73ddeb209ea79ebee493cc55dc377ff69e1577385d31930ca63db35bbffe34caea1bd41c05  riscv.patch
3fc8fbd2969b2c45730a5432430a5979572ce1447db124f8f1b8755633e7e109dd2ec9ecb1d7fa0a961ae0630995e22caafe7e362b74903d419b6c57bbbd2dd2  strerror.patch
4528ccee466bdfed4d2cd6095fb0b2f4f2b2e662c59a8d1a9f26877b4e9d2a8d0b7776932ed87d01dc7ac1e17a29dd5ac47651b91975a33cfe46516d1590f829  werror.patch
"
